import { sysLogin, profile, sysUser } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken() || '',
  userInfo: ''
}
const mutations = {
  setLogin(state, val) {
    state.token = val
    setToken(val)
  },
  setUserInfo(state, val) {
    state.userInfo = val
  },
  logout(state) {
    state.userInfo = ''
    state.token = ''
    removeToken()
  }
}
const actions = {
  // 登陆处理
  async getLogin({ commit }, val) {
    const res = await sysLogin(val)
    alert('登陆成功')
    commit('setLogin', res.data)
    // console.log(res.data)
  },
  // 获取用户资料
  async getUserInfo(store) {
    const res = await profile()
    // console.log(res)
    const res1 = await sysUser(res.data.userId)
    // console.log(res1)
    store.commit('setUserInfo', Object.assign({}, res.data, res1.data))
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
